/*
 * @Auther        : SR
 * @Date          : 2022-02-25 13:24:16
 * @LastEditTime  : 2022-02-27 16:11:20
 */
package windows

import (
	"ipfs-browser/src/es7"
	"log"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
	"ipfs-browser/src/IPFS"
)

func downloadScreen(win fyne.Window) fyne.CanvasObject {
	downloadPath := widget.NewLabel("download path")

	fileInfo := widget.NewLabel("file Info") //搜索栏下方的显示信息

	folderSelect := widget.NewButton("Folder Select", func() {

		fd := dialog.NewFolderOpen(func(list fyne.ListableURI, err error) {
			if err != nil {
				dialog.ShowError(err, win)
				return
			}
			if list == nil {
				log.Println("Cancelled")
				return
			}

			//! filePathOpened(reader)
			log.Println("select path:", list.Path())
			downloadPath.SetText(list.Path())
			// fileInfo.SetText("file name:" + reader.URI().Name())
		}, win)
		// fd.SetFilter(storage.NewExtensionFileFilter([]string{".png", ".jpg", ".jpeg"}))
		fd.Show()
	})

	inputCid := widget.NewEntry()
	inputCid.SetText("input cid")
	inputCid.Wrapping = fyne.TextWrapOff

	downloadButton := widget.NewButton("Download", func() {
		m, _ := es7.QueryByCid(inputCid.Text)
		p := downloadPath.Text + "/" + m.Name
		log.Println("search result:", m)
		IPFS.Download(inputCid.Text, p)
		log.Println("\nDownloaded file \ncid : " + inputCid.Text +
			"\nfile path : " + p)
	})

	return container.NewBorder(
		container.NewBorder(
			nil, nil,
			container.NewHBox(folderSelect, downloadPath),
			container.NewHBox(
				widget.NewSeparator(), //添加filePath的边界
				inputCid,
				downloadButton,
			),
		), nil, nil, nil,
		container.NewVBox(widget.NewSeparator(), fileInfo),
	)
}
